/**
 * 自动生成 WxNewsReply
 * 
 * Public/js/mp/wx_news_reply.js
 * Zix @ 2015-09-06
 */

var WxNewsReply = {
  config: {
    pid : 0 ,
    multi_data : {} ,
  },
  init: function (config) {
    var _this = this;
    $.extend(_this.config, config);
    Layout.set_menu_select(_this.config.menu_id);

    loading();
    _this.init_btn();
    _this.init_grid();
    sessionStorage.removeItem("scroll_top");
  },
  
  set_modal: function () {
    var _this = this;
    var scroll_top = sessionStorage.getItem("scroll_top");
    switch (_this.config.action) {
      case 'read' :
        $('#read_modal').show();
        $('#add_edit_modal').hide();

        if (isset(scroll_top)) {
          $(window).scrollTop(scroll_top);
        }
        break;
      case 'create':
      case 'update':
        sessionStorage.setItem('scroll_top', $(window).scrollTop());
        
        $('#read_modal').hide();
        $('#add_edit_modal').show();
        break;
    }
  },
  
  init_btn: function () {
    var _this = this;
    
    //封面
    $('#icon_upload_btn').Huploadify({
      uploader : _this.config.upload_icon_url , //上传文件
      itemTemplate : '',
      onUploadSuccess : function(file, responseText){
        var ret = $.parseJSON( responseText );
        tips( ret.info );
        if ( ret.status ) {
          _this.set_img_preview('icon' , ret.data.savePath );
        }
      }
    });
    
    //查询按钮
    $(document).on('click', '#search_btn', function (e) {
      e.preventDefault();
      var query_param = $('#data_table').TableGrid('getparam');
      query_param.page = 1;
      
      $('#data_table').TableGrid('param', query_param);
      $('#data_table').TableGrid('reload');
    });
    
    //关闭新增或编辑
    $(document).on('click', '#add_edit_close_btn', function (e) {
      e.preventDefault();
      _this.config.action = 'read';
      _this.set_modal();
      _this.config.pid = 0 ;
    });
    
    //打开添加窗口
    $(document).on('click', '.load_add_modal_btn', function (e) {
      e.preventDefault();
      _this.config.action = 'create';
      $('#submit_form').attr('action', _this.config.create_url);
      $('#submit_form')[0].reset();
      $('input[name="type"]').prop('disabled' , false );
      fillform( $('#submit_form') , _this.config.row );
      _this.set_img_preview('icon' , '' );
      $('#action_label').html('新增');
      $('#add_edit_modal').removeClass('yellow').addClass('red');
      _this.set_modal();
    });

    //打开编辑窗口
    $(document).on('click', '.edit_btn', function (e) {
      e.preventDefault();
      
      var index = $('.edit_btn').index( $(this) );
      var data = $('#data_table').TableGrid('getrow' , index);
      _this.config.pid = 0 ;
      $('.hide_line').show();
      $('input[name="type"]').prop('disabled' , true);
      

      fillform( $('#submit_form') , data );
      
      _this.set_img_preview('icon' , data.icon );
      
      $('#submit_form').attr('action', _this.config.update_url + '/' + data['id'] );
      _this.config.action = 'update';
            
      $('#action_label').html('编辑');
      $('#add_edit_modal').removeClass('red').addClass('yellow');
      _this.set_modal();  
      
    });
    
    //添加多图文
    $(document).on('click' , '.add_multi_btn' , function(ret){
      _this.config.pid = $(this).data('pid');
      $('.hide_line').hide();
      $('.load_add_modal_btn').trigger('click');
    });
    
    //编辑多图文
    $(document).on('click', '.edit_multi_btn', function (e) {
      e.preventDefault();
      var pid = $(this).data('pid');
      var id = $(this).data('id');
      var index = $('#content_' + pid ).find('.edit_multi_btn').index( $(this) );
      var data = _this.config.multi_data[pid][index];

      $('.hide_line').hide();
      fillform( $('#submit_form') , data );      
      _this.set_img_preview('icon' , data.icon );
      
      $('#submit_form').attr('action', _this.config.update_url + '/' + id );
      _this.config.action = 'update';
            
      $('#action_label').html('编辑');
      $('#add_edit_modal').removeClass('red').addClass('yellow');
      _this.set_modal();  

    });

    //提交
    $(document).on('click', '#submit_btn', function (e) {
      e.preventDefault();

      var _thisbtn = $(this);
      var url = $('#submit_form').attr('action');
      var data = $('#submit_form').serializeObject();
      
      if (validform($('#submit_form'))) {
        data.pid = _this.config.pid ;
        data.wx_id = _this.config.wx_id ;
                        
        $.post(url, data, function (ret) {
          tips(ret.info) ;
          if (ret.status) {
            _this.config.action = 'read';
            _this.set_modal();
            var query_param = $('#data_table').TableGrid('getparam');
            query_param.page = 1 ;
            $('#data_table').TableGrid('param' , query_param);
            $('#data_table').TableGrid('reload');
          }
        });
      }
    });
    
    //批量删除按钮
    $(document).on('click', '.del-btn', function (e) {
      e.preventDefault();
      var select_id = new Array();
      $("input[name='tablegrid-select[]']").each(function (index) {
        if ($(this).prop('checked')) {
          //取表数据
          var row = $('#data_table').TableGrid('getrow', index);
          select_id.push(row.id);
        }
      });

      if (select_id.length == 0) {
        tips('请选择数据');
      } else {
        _this.config.del_id = select_id.join(',');
        _this.del_data();
      }
    });

    //单个删除按钮
    $(document).on('click', '.del_one_btn', function (e) {
      e.preventDefault();
      var index = $('.del_one_btn').index( $(this) );
      var data = $('#data_table').TableGrid('getrow' , index);
      _this.config.del_id = data['id'];
      _this.del_data();
    });
    
    //单个删除按钮
    $(document).on('click', '.del_multi_btn', function (e) {
      e.preventDefault();
      var id = $(this).data('id');
      _this.config.del_id = id ;
      _this.del_data();
    });
    
    //打开或关闭多图文
    $(document).on('click' , '.toggle_mutli' , function(e){
      e.preventDefault();
      var id = $(this).data('id');
      var tr_id = 'tr_' + $(this).data('id');
      var btn_id = 'btn_' + $(this).data('id');
      var content_id = 'content_' + $(this).data('id');
      
      if( $('#' + tr_id ).length == 0 ) {
        var tr = $(this).parents('tr');
        var len = tr.find('td').length - 1;
        var btn_html = '<div style="margin-bottom:16px; text-align:right"><a class="btn btn-danger add_multi_btn" data-pid="'+ id +'" href="#" ><i class="fa fa-plus"></i> 添加多图文</a></div>';
        var html = '<tr id="'+ tr_id +'" class="multi_tr" style="display:none"><td>&nbsp;</td><td colspan="'+ len  +'">'+ btn_html +'<div id="'+ content_id +'"></div></td></tr>';
        tr.after(html); 
      }
      $('.multi_tr').hide();
      $('#'+ tr_id ).toggle();
      if( !$('#'+ tr_id ).is(':hidden') ) {
        _this.config.pid = id ;
        _this.load_multi() ;
      }
           
    });
    

  },
  
  load_multi : function(){
    var _this = this ;
    var url = _this.config.read_by_pid_url + _this.config.pid  ;
    $.get( url , function(ret){
      _this.config.multi_data[_this.config.pid] = ret.rows ;
      $('#content_' + _this.config.pid ).html(ret.html)
    }); 
  },
  
  init_grid: function () {
    var _this = this;
    $('#data_table').TableGrid({
      url: _this.config.read_url,
      select_all: false,
      load_after_init: true,
      row_style: function (row, index) {
        if (row.status != 1 ) {
          return 'info';
        }
      }
    });
  },
  
  del_data: function (ids) {
    var _this = this;

    sure.init('确定删除吗？', function () {
      var url = _this.config.delete_url;
      var data = {ids: _this.config.del_id};

      $.post(url, data, function (ret) {
        tips(ret.info);
        if (ret.status) {
          $('#data_table').TableGrid('reload');
        }
      });
    });
  },
  
  set_img_preview : function(input_name , img_save_path , not_show_delete_btn ){
    if ( empty(img_save_path) ) {
      $('#'+ input_name +'_preview').html( '' );
      return ;
    }
    var img_ab_path = get_domain() + img_save_path ;
    $('input[name="'+ input_name +'"]').val( img_save_path );
    var img_html = '<a target="_blank" href="'+ img_ab_path +'" ><img src="'+ img_ab_path +'"></a>' ;
    var del_btn_html = '';
    if ( isset(not_show_delete_btn) ) {
      var del_btn_html = '<a href="'+ img_save_path +'" class="del-icon-btn" data-input-name="'+ input_name +'"><i class="fa fa-times "></i></a>';
    }
    $('#'+ input_name +'_preview').html( img_html + del_btn_html );
  }
};

var opt_edit = function(value, row, index) {
  WxNewsReply.config.pid = 0 ;
	return '<a class="btn btn-warning edit_btn" data-id="' + row.id
			+ '" href="#" taget="_blank" title="编辑"><i class="fa fa-pencil-square-o "></i> 编辑</a>';
}

var opt_delete = function(value, row, index) {
  if (  row.type == 1 || row.kids == 0 ) {
    return '<a class="btn btn-danger del_one_btn" data-id="' + row.id
        + '" href="#" taget="_blank" title="删除"><i class="fa fa-times "></i> 删除</a>';
  } else {
    return '';
  }
}

var format_title = function( value , row , index ){
  return '<a href="'+ row.url +'" target="_blank">'+ row.title +'</a>';
}

var opt_mutli = function( value , row , index ){
  if ( row.type == 1 ) {
    return '';
  } else {
    return '<a href="#" class="btn btn-info toggle_mutli" data-id="'+ row.id +'">查看多图文</a>';
  }
}



